package com.example.main;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ParseRegionUtil {


    /**
     * 读入TXT文件
     */
    public static void readFile() {
        String pathname = "C:\\dingijahui\\AndroidStudioProjects\\main\\app\\src\\main\\java\\com\\example\\main\\region.txt"; // 绝对路径或相对路径都可以，写入文件时演示相对路径,读取以上路径的input.txt文件
        //防止文件建立或读取失败，用catch捕捉错误并打印，也可以throw;
        //不关闭文件会导致资源的泄露，读写文件都同理
        //Java7的try-with-resources可以优雅关闭文件，异常时自动关闭文件；详细解读https://stackoverflow.com/a/12665271
        try (FileReader reader = new FileReader(pathname);
             BufferedReader br = new BufferedReader(reader) // 建立一个对象，它把文件内容转成计算机能读懂的语言
        ) {
            String line;
            //网友推荐更加简洁的写法
            while ((line = br.readLine()) != null) {
                String[] strings = line.split("\t");
                // 一次读入一行数据

//                System.out.println(strings[0] + " r:" + strings[1]);
                parseData(strings[0].trim(), strings[1].trim());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static List<RegionBean> regionBeanList = new ArrayList<>();

    public static void parseData(String code, String name) {
        if (code.endsWith("0000")) {
            RegionBean regionBean = new RegionBean();
            regionBean.setRegionID(code);
            regionBean.setRegionName(name);
            regionBean.setRegionBeanList(new ArrayList<RegionBean>());
            regionBeanList.add(regionBean);
        } else if (code.endsWith("00")) {
            String start = code.substring(0, 2);
            for (int i = 0; i < regionBeanList.size(); i++) {
                String parent = regionBeanList.get(i).getRegionID().substring(0, 2);
                if (start.equals(parent)) {
                    RegionBean regionBean = new RegionBean();
                    regionBean.setRegionID(code);
                    regionBean.setRegionName(name);
                    regionBean.setRegionBeanList(new ArrayList<RegionBean>());
                    regionBeanList.get(i).getRegionBeanList().add(regionBean);
                }
            }
        }else {
            String start = code.substring(0, 2);
            for (int i = 0; i < regionBeanList.size(); i++) {
                String parent = regionBeanList.get(i).getRegionID().substring(0, 2);
                if (start.equals(parent)) {
                    List<RegionBean> secondList = regionBeanList.get(i).getRegionBeanList();
                    String sStart = code.substring(2,4);
                    for (int j=0;j<secondList.size();j++){
                        String sParent = secondList.get(j).getRegionID().substring(2,4);
                        if (sStart.equals(sParent)){
                            RegionBean rb = new RegionBean();
                            rb.setRegionID(code);
                            rb.setRegionName(name);
                            secondList.get(j).getRegionBeanList().add(rb);
                        }
                    }
                }
            }
        }


    }


}
